package com.ryenlii.datatrans.core.util;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.multi.qrcode.QRCodeMultiReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class QRCodeDetector {

    public static List<String> readQRCodes(BufferedImage image) throws NotFoundException {
        List<String> qrCodeData = new ArrayList<>();
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeMultiReader reader = new QRCodeMultiReader();
        for (Result result : reader.decodeMultiple(bitmap, null)) {
            qrCodeData.add(result.getText());
        }
        return qrCodeData;
    }

    public static void main(String[] args) {
        try {
            String path = "D:\\data\\46870606-4e12-4646-ba09-39a40cc2599d.pdf";
            BufferedImage image = ImageIO.read(new File(path));
            List<String> qrCodeData = readQRCodes(image);
            System.out.println("二维码数据： " + qrCodeData);
        } catch (IOException | NotFoundException e) {
            System.out.println("读取二维码失败： " + e.getMessage());
        }
    }
}
